package cn.yunyichina.scheduler.jobs.common.service;

import cn.yunyichina.scheduler.jobs.common.cache.RuleConfigManager;
import cn.yunyichina.scheduler.jobs.common.jackson.Jackson;
import cn.yunyichina.scheduler.jobs.entity.message.MsgPushEntity;
import cn.yunyichina.scheduler.jobs.entity.message.RulePush;
import cn.yunyichina.scheduler.jobs.entity.vo.MessagePushParamsVo;
import cn.yunyichina.scheduler.jobs.thread.pool.MessagePushThreadPool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Lullaby on 2016/8/17.
 */
@Service
public class MessageService {

    private static Logger logger = LoggerFactory.getLogger(MessageService.class);

    public static final String WECHAT = "wechat";

    public static final String ALIPAY = "alipay";

    public static final int IS_NEED_PUSH_MSG_YES = 1;


    public static final int IS_NEED_PUSH_MSG_NO = 0;

  
    public static final int ACTION_TYPE_BIND_CARD_SUCCESS = 1;

 
    public static final int ACTION_TYPE_CREATE_CARD_SUCCESS = 2;


    public static final int ACTION_TYPE_PREDAY_VISIT = 3;


    public static final int ACTION_TYPE_CURDAY_VISIT = 4;

  
    public static final int ACTION_TYPE_LOCK_RES_SUCCESS = 5;

   
    public static final int ACTION_TYPE_ON_DUTY_PAY_SUCCESS = 6;

 
    public static final int ACTION_TYPE_APPOINT_PAY_SUCCESS = 7;

 
    public static final int ACTION_TYPE_APPOINT_PAY_FAIL = 8;

  
    public static final int ACTION_TYPE_APPOINT_PAY_EXP = 9;

  
    public static final int ACTION_TYPE_CANCEL_ON_DUTY = 10;

   
    public static final int ACTION_TYPE_CANCEL_APPOINTMENT = 11;

  
    public static final int ACTION_TYPE_REFUND_SUCCESS = 12;

  
    public static final int ACTION_TYPE_REFUND_FAIL = 13;

  
    public static final int ACTION_TYPE_REFUND_EXCEPTION = 14;

  
    public static final int ACTION_TYPE_STOP_VISIT = 15;

    
    public static final int ACTION_TYPE_WAIT_VISIT = 16;

    
    public static final int ACTION_TYPE_CLINIC_PAY_SUCCESS = 17;

   
    public static final int ACTION_TYPE_CLINIC_PAY_FAIL = 18;

   
    public static final int ACTION_TYPE_CLINIC_PAY_EXP = 19;

  
    public static final int ACTION_TYPE_DEPOSIT_PAY_SUCCESS = 20;

    
    public static final int ACTION_TYPE_DEPOSIT_PAY_FAIL = 21;

    
    public static final int ACTION_TYPE_DEPOSIT_PAY_EXP = 22;

    
    public static final int ACTION_TYPE_GENERATE_REPORT = 23;

   
    public static final int ACTION_TYPE_CLINIC_REFUND_SUCCESS = 24;

   
    public static final int ACTION_TYPE_CLINIC_PART_REFUND_SUCCESS = 25;

    
    public static final int ACTION_TYPE_CLINIC_SUCCESS_COMMENT = 26;

    /**
     * 完善用户资料的消息
     */
    public static final int ACTION_TYPE_PERFECT_USER_INFO = 27;

    /**
     * 医保预约挂号提醒缴费消息推送
     */
    public static final int ACTION_TYPE_MEDICAL_RESERVE_REGISTER_PAY = 28;

    @Resource
    private RuleConfigManager ruleConfigManager;

    public void pushMessage(MsgPushEntity entity, int type) {
        MessagePushParamsVo vo = entity.convertMessagePushParams();
        logger.info("消息推送实体[{}]，类型[{}]", Jackson.getBaseJsonData(entity), type);
        RulePush rulePush = ruleConfigManager.getRulePushByHospitalId(vo.getHospitalId(), vo.getPlatformType());
        if (rulePush != null) {
            logger.info("消息推送规则配置[{}]", Jackson.getBaseJsonData(rulePush));
            String code = null;
            int isPush;
            switch (type) {
                case ACTION_TYPE_BIND_CARD_SUCCESS:
                    isPush = rulePush.getBindCardSuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getBindCardSucCode();
                    }
                    break;
                case ACTION_TYPE_CREATE_CARD_SUCCESS:
                    isPush = rulePush.getCreateCardSuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getCreateCardSucCode();
                    }
                    break;
                case ACTION_TYPE_LOCK_RES_SUCCESS:
                    isPush = rulePush.getLockResSuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getLockResSucCode();
                    }
                    break;
                case ACTION_TYPE_PREDAY_VISIT:
                    isPush = rulePush.getPredayVisit();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getPredayVisitCode();
                    }
                    break;
                case ACTION_TYPE_CURDAY_VISIT:
                    isPush = rulePush.getCurdayVisit();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getCurdayVisitCode();
                    }
                    break;
                case ACTION_TYPE_ON_DUTY_PAY_SUCCESS:
                    isPush = rulePush.getOnDutyPaySuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getOnDutyPaySucCode();
                    }
                    break;
                case ACTION_TYPE_APPOINT_PAY_SUCCESS:
                    isPush = rulePush.getAppointPaySuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getAppointPaySucCode();
                    }
                    break;
                case ACTION_TYPE_APPOINT_PAY_FAIL:
                    isPush = rulePush.getAppointPayFail();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getAppointPayFailCode();
                    }
                    break;
                case ACTION_TYPE_APPOINT_PAY_EXP:
                    isPush = rulePush.getAppointPayExp();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getAppointPayExpCode();
                    }
                    break;
                case ACTION_TYPE_CANCEL_ON_DUTY:
                    isPush = rulePush.getCancelOnDuty();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getCancelOnDutyCode();
                    }
                    break;
                case ACTION_TYPE_CANCEL_APPOINTMENT:
                    isPush = rulePush.getCancelAppointment();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getCancelAppointmentCode();
                    }
                    break;
                case ACTION_TYPE_REFUND_SUCCESS:
                    isPush = rulePush.getRefundSuccess();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getRefundSuccessCode();
                    }
                    break;
                case ACTION_TYPE_REFUND_FAIL:
                    isPush = rulePush.getRefundFail();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getRefundFailCode();
                    }
                    break;
                case ACTION_TYPE_REFUND_EXCEPTION:
                    isPush = rulePush.getRefundException();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getRefundExceptionCode();
                    }
                    break;
                case ACTION_TYPE_STOP_VISIT:
                    isPush = rulePush.getStopVisit();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getStopVisitCode();
                    }
                    break;
                case ACTION_TYPE_WAIT_VISIT:
                    isPush = rulePush.getWaitVisit();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getWaitVisitCode();
                    }
                    break;
                case ACTION_TYPE_CLINIC_PAY_SUCCESS:
                    isPush = rulePush.getClinicPaySuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getClinicPaySucCode();
                    }
                    break;
                case ACTION_TYPE_CLINIC_PAY_FAIL:
                    isPush = rulePush.getClinicPayFail();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getClinicPayFailCode();
                    }
                    break;
                case ACTION_TYPE_CLINIC_PAY_EXP:
                    isPush = rulePush.getClinicPayExp();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getClinicPayExpCode();
                    }
                    break;
                case ACTION_TYPE_DEPOSIT_PAY_SUCCESS:
                    isPush = rulePush.getPayDepositSuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getPayDepositSucCode();
                    }
                    break;
                case ACTION_TYPE_DEPOSIT_PAY_FAIL:
                    isPush = rulePush.getPayDepositFail();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getPayDepositFailCode();
                    }
                    break;
                case ACTION_TYPE_DEPOSIT_PAY_EXP:
                    isPush = rulePush.getPayDepositExp();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getPayDepositExpCode();
                    }
                    break;
                case ACTION_TYPE_GENERATE_REPORT:
                    isPush = rulePush.getGenerateReport();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getGenerateReportCode();
                    }
                    break;
                case ACTION_TYPE_CLINIC_REFUND_SUCCESS:
                    isPush = rulePush.getClinicRefundSuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getClinicRefundSucCode();
                    }
                    break;
                case ACTION_TYPE_CLINIC_PART_REFUND_SUCCESS:
                    isPush = rulePush.getClinicPartRefundSuc();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getClinicPartRefundSucCode();
                    }
                    break;
                case ACTION_TYPE_CLINIC_SUCCESS_COMMENT:
                    isPush = rulePush.getSendComment();
                    if (isPush == IS_NEED_PUSH_MSG_YES) {
                        code = rulePush.getSendCommentCode();
                    }
                    break;
                case ACTION_TYPE_PERFECT_USER_INFO:
                    break;
                case ACTION_TYPE_MEDICAL_RESERVE_REGISTER_PAY:
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                    break;
                default:
                    break;
            }

            if (StringUtils.isNotBlank(code)) {
                vo.setTemplateCode(code);
                MessagePushThreadPool.messageThreadPool.execute(new MessagePushRunnable(vo));
            }
        } else {
            logger.warn("消息推送未获取到规则配置");
        }
    }

}
